<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/3/24
 * Time: 15:18
 */

namespace Api\Domain;

use Bbs\Api\BbsMemberApi;
use Bbs\Api\ForumAttachmentApi;
use Bbs\Api\ForumPostApi;
use Bbs\Api\ForumPostcommentApi;
use Bbs\Api\ForumThreadApi;
use Bbs\Model\ForumPostModel;

class BbsUserCenterDomain extends BaseDomain{

    protected $business_code = 'BbsMyCenter';

    protected $allowType = array("json", "rss", "html");

    public function userInfo($uid,&$arr=null){

        $result = apiCall(BbsMemberApi::GET_USER_INFO,array($uid));

        if(!is_null($arr) && $result['status']){
            $arr['user_info'] = $result['info'];
        }

        return $result;

    }

    public function getUserInfo(){

        if(IS_POST){
            $this->checkVersion(100);
            addLog("BbsUserCenter/getUserInfo",$_GET,$_POST,"应用" . $this->client_id . "调用用户信息查询接口");
            $uid = $this->_post('uid','','用户ID缺失');

            $result = $this->userInfo($uid);

            if($result['status']){
                $this->apiReturnSuc($result['info']);
            }else{
                $this->apiReturnErr($result['info']);
            }
        }else{
            $this->apiReturnErr('只支持POST请求');
        }


    }

    public function queryThread(){

        $this->checkVersion(100);
        addLog("BbsUserCenter/queryThread",$_GET,$_POST,"应用" . $this->client_id . "调用我的主题帖查询接口");

        if(IS_POST){
            $uid = $this -> _post('uid',0,'用户id缺失');
            $page_no = $this -> _post('page_no',1);
            $page_size = $this -> _post('page_size',6);
            $img_num = $this -> _post('img_num',3);

            $map = array(
                'author_id' => $uid,
            );

            $page = array(
                'curpage'=>$page_no,
                'size'=>$page_size,
            );

            $order = "dateline desc";

            $result = apiCall(ForumThreadApi::QUERY_WITH_COUNT,array($map,null,$page,$order));

            if($result['status']){
                foreach($result['info']['list'] as &$vo){
                    $time = time() - $vo['dateline'];
                    $vo['dateline_show'] = getDateInfo($time,$vo['dateline']);

                    //取用户信息
                    $this->userInfo($vo['author_id'],$vo);

                    $map = array(
                        'tid' => $vo['tid'],
                    );
                    $field = "aid";
                    $res = apiCall(ForumAttachmentApi::QUERY_NO_PAGING,array($map,false,$field));
                    if($res['status']){
                        $vo['img'] = array();
                        foreach($res['info'] as $v){
                            array_push($vo['img'],$v['aid']);
                        }
                    }else{
                        $this -> apiReturnErr($res['info']);
                    }
                }
                $this -> apiReturnSuc($result['info']);

            }else{
                $this -> apiReturnErr($result['info']);
            }
        }

    }

    public function queryComment(){

        $this->checkVersion(100);
        addLog("BbsUserCenter/queryComment",$_GET,$_POST,"应用" . $this->client_id . "调用我的评论查询接口");

        if(IS_POST){
            $uid = $this -> _post('uid',0,'用户id缺失');
            $page_no = $this -> _post('page_no',1);
            $page_size = $this -> _post('page_size',6);

            $map = array(
                'itboye_forum_postcomment.author_id' => $uid,
            );
            $page = array(
                'curpage'=>$page_no,
                'size'=>$page_size,
            );
            $order = "itboye_forum_postcomment.dateline desc";
            $field = "itboye_forum_post.author as post_author,itboye_forum_post.author_id as post_author_id,itboye_forum_post.dateline as post_dateline,itboye_forum_post.message as post_message,itboye_forum_post.status as post_status,itboye_forum_post.position as post_position,itboye_forum_post.tid,itboye_forum_post.fid";
            $field = $field.",itboye_forum_postcomment.author as comment_author,itboye_forum_postcomment.author_id as comment_author_id,itboye_forum_postcomment.dateline as comment_dateline,itboye_forum_postcomment.comment as comment_comment,itboye_forum_postcomment.pid";
            $result = apiCall(ForumPostcommentApi::QUERY_WITH_POST,array($map,$page,$order,false,$field));
            if($result['status']){

                foreach($result['info']['list'] as &$vo) {
                    $time = time() - $vo['post_dateline'];
                    $vo['post_dateline_show'] = getDateInfo($time, $vo['post_dateline']);
                    $time = time() - $vo['comment_dateline'];
                    $vo['comment_dateline_show'] = getDateInfo($time, $vo['comment_dateline']);

                    //取用户信息
                    $this->userInfo($vo['comment_author_id'], $vo);

                    $map = array(
                        'tid' => $vo['tid'],
                    );
                    $res = apiCall(ForumThreadApi::GET_INFO,array($map));
                    if($res['status']){
                        if($res['info'] == ""){
                            $this -> apiReturnErr("数据有误");
                        }else{
                            $vo['thread_status'] = $res['info']['status'];
                        }
                    }else{
                        $this -> apiReturnErr($res['info']);
                    }
                }

                $this -> apiReturnSuc($result['info']);
            }else{
                $this -> apiReturnErr($result['info']);
            }
        }

    }

    public function queryPost(){

        $this->checkVersion(100);
        addLog("BbsUserCenter/queryPost",$_GET,$_POST,"应用" . $this->client_id . "调用我的帖子查询接口");

        if(IS_POST){
            $uid = $this -> _post('uid',0,'用户id缺失');
            $page_no = $this -> _post('page_no',1);
            $page_size = $this -> _post('page_size',6);
            $img_num = $this -> _post('img_num',3);

            $map = array(
                'itboye_forum_post.author_id' => $uid,
            );
            $page = array(
                'curpage'=>$page_no,
                'size'=>$page_size,
            );
            $order = "itboye_forum_post.dateline desc";
            $field = "itboye_forum_thread.tid,itboye_forum_thread.fid,itboye_forum_thread.author as thread_author,itboye_forum_thread.author_id as thread_author_id,itboye_forum_thread.subject as thread_subject,itboye_forum_thread.dateline as thread_dateline,itboye_forum_thread.views as thread_views,itboye_forum_thread.replies as thread_replies,itboye_forum_thread.digest as thread_digest,itboye_forum_thread.stick as thread_stick,itboye_forum_thread.status as thread_status,itboye_forum_thread.likes as thread_likes";
            $field = $field.",itboye_forum_post.pid,itboye_forum_post.author as post_author,itboye_forum_post.author_id as post_author_id,itboye_forum_post.dateline as post_dateline,itboye_forum_post.message as post_message,itboye_forum_post.invisible as post_invisible,itboye_forum_post.status as post_status,itboye_forum_post.comment as post_comment,itboye_forum_post.position as post_position";
            $result = apiCall(ForumPostApi::QUERY_WITH_THREAD,array($map,$page,$order,false,$field));

            if($result['status']){
                foreach($result['info']['list'] as &$vo){
                    $time = time() - $vo['post_dateline'];
                    $vo['post_dateline_show'] = getDateInfo($time,$vo['post_dateline']);

                    //取用户信息
                    $this->userInfo($vo['post_author_id'],$vo);

                    $map = array(
                        'tid' => $vo['tid'],
                    );
                    $field = "aid";
                    $res = apiCall(ForumAttachmentApi::QUERY_NO_PAGING,array($map,false,$field));
                    if($res['status']){
                        $vo['img'] = array();
                        foreach($res['info'] as $v){
                            array_push($vo['img'],$v['aid']);
                        }
                    }else{
                        $this -> apiReturnErr($res['info']);
                    }
                }
                $this -> apiReturnSuc($result['info']);

            }else{
                $this -> apiReturnErr($result['info']);
            }
        }

    }

    public function replyThread(){

        $this->checkVersion(100);
        addLog("BbsUserCenter/replyThread",$_GET,$_POST,"应用" . $this->client_id . "调用查看主题回复接口");

        if(IS_POST){
            $uid = $this -> _post('uid',0,'用户id缺失');
            $page_no = $this -> _post('page_no',1);
            $page_size = $this -> _post('page_size',6);
            $img_num = $this -> _post('img_num',3);

            $map = array(
                'itboye_forum_thread.author_id' => $uid,
                'itboye_forum_post.author_id' => array('NEQ',$uid),
            );
            $page = array(
                'curpage'=>$page_no,
                'size'=>$page_size,
            );
            $order = "itboye_forum_post.dateline desc";
            $field = "itboye_forum_thread.tid,itboye_forum_thread.fid,itboye_forum_thread.author as thread_author,itboye_forum_thread.author_id as thread_author_id,itboye_forum_thread.subject as thread_subject,itboye_forum_thread.dateline as thread_dateline,itboye_forum_thread.views as thread_views,itboye_forum_thread.replies as thread_replies,itboye_forum_thread.digest as thread_digest,itboye_forum_thread.stick as thread_stick,itboye_forum_thread.status as thread_status,itboye_forum_thread.likes as thread_likes";
            $field = $field.",itboye_forum_post.pid,itboye_forum_post.author as post_author,itboye_forum_post.author_id as post_author_id,itboye_forum_post.dateline as post_dateline,itboye_forum_post.message as post_message,itboye_forum_post.invisible as post_invisible,itboye_forum_post.status as post_status,itboye_forum_post.comment as post_comment,itboye_forum_post.position as post_position";
            $result = apiCall(ForumPostApi::QUERY_WITH_THREAD,array($map,$page,$order,false,$field,));

            if($result['status']){
                foreach($result['info']['list'] as &$vo){
                    $time = time() - $vo['post_dateline'];
                    $vo['post_dateline_show'] = getDateInfo($time,$vo['post_dateline']);

                    //取用户信息
                    $this->userInfo($vo['post_author_id'],$vo);

                    $map = array(
                        'tid' => $vo['tid'],
                    );
                    $field = "aid";
                    $res = apiCall(ForumAttachmentApi::QUERY_NO_PAGING,array($map,false,$field));
                    if($res['status']){
                        $vo['img'] = array();
                        foreach($res['info'] as $v){
                            array_push($vo['img'],$v['aid']);
                        }
                    }else{
                        $this -> apiReturnErr($res['info']);
                    }
                }
                $this -> apiReturnSuc($result['info']);

            }else{
                $this -> apiReturnErr($result['info']);
            }
        }

    }

    public function replyPost(){

        $this->checkVersion(100);
        addLog("BbsUserCenter/replyPost",$_GET,$_POST,"应用" . $this->client_id . "调用查看帖子回复接口");

        if(IS_POST){
            $uid = $this -> _post('uid',0,'用户id缺失');
            $page_no = $this -> _post('page_no',1);
            $page_size = $this -> _post('page_size',6);

            $map = array(
                'itboye_forum_post.author_id' => $uid,
                'itboye_forum_postcomment.author_id' => array('NEQ',$uid),
            );
            $page = array(
                'curpage'=>$page_no,
                'size'=>$page_size,
            );
            $order = "itboye_forum_postcomment.dateline desc";
            $field = "itboye_forum_post.author as post_author,itboye_forum_post.author_id as post_author_id,itboye_forum_post.dateline as post_dateline,itboye_forum_post.message as post_message,itboye_forum_post.status as post_status,itboye_forum_post.position as post_position,itboye_forum_post.tid,itboye_forum_post.fid";
            $field = $field.",itboye_forum_postcomment.author as comment_author,itboye_forum_postcomment.author_id as comment_author_id,itboye_forum_postcomment.dateline as comment_dateline,itboye_forum_postcomment.comment as comment_comment,itboye_forum_postcomment.pid";
            $result = apiCall(ForumPostcommentApi::QUERY_WITH_POST,array($map,$page,$order,false,$field));
            if($result['status']){

                foreach($result['info']['list'] as &$vo) {
                    $time = time() - $vo['post_dateline'];
                    $vo['post_dateline_show'] = getDateInfo($time, $vo['post_dateline']);
                    $time = time() - $vo['comment_dateline'];
                    $vo['comment_dateline_show'] = getDateInfo($time, $vo['comment_dateline']);

                    //取用户信息
                    $this->userInfo($vo['comment_author_id'], $vo);

                    $map = array(
                        'tid' => $vo['tid'],
                    );
                    $res = apiCall(ForumThreadApi::GET_INFO,array($map));
                    if($res['status']){
                        if($res['info'] == ""){
                            $this -> apiReturnErr("数据有误");
                        }else{
                            $vo['thread_status'] = $res['info']['status'];
                        }
                    }else{
                        $this -> apiReturnErr($res['info']);
                    }
                }

                $this -> apiReturnSuc($result['info']);
            }else{
                $this -> apiReturnErr($result['info']);
            }
        }

    }

}